﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

namespace SAP
{
    public partial class Popup_ReceptionSheet : System.Web.UI.Page
    {

        private static DataTable dtDetail;
        private static string GeneralID = "";
        private static string dbType;
        private static int RSID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dbType = Session[VSM.Utils.AppConstants.ID_Dealer].ToString() == "VSM" ? VSM.Utils.AppConstants.VSM : VSM.Utils.AppConstants.DEALER;
                GeneralID = Request.QueryString["GeneralID"] == null ? "" : Request.QueryString["GeneralID"];
                hf_GeneralID.Value = GeneralID;
                loadReceptionSheet();
                editItemUpdatePanel.Update();
            }
        }


        private void loadReceptionSheet() {
            if (GeneralID == "")
                return;
            Services sv = new Services();
            dtDetail = sv.ExecuteQuery(dbType, "select * from U_ReceptionSheet where GeneralID = " + GeneralID).Tables[0];

            if (dtDetail.Rows.Count > 0)
            {

                RSID = Int32.Parse(dtDetail.Rows[0]["RSID"].ToString());
                txt_Lamps.Text = dtDetail.Rows[0]["Lamps"].ToString();
                txt_Mirrors.Text = dtDetail.Rows[0]["Mirrors"].ToString();
                txt_Wheels.Text = dtDetail.Rows[0]["WheelsTires"].ToString();
                txt_Antenna.Text = dtDetail.Rows[0]["Antenna"].ToString();
                txt_Radio.Text = dtDetail.Rows[0]["Radio"].ToString();
                txt_Cigar.Text = dtDetail.Rows[0]["CigarLighter"].ToString();
                txt_ToolsSet.Text = dtDetail.Rows[0]["ToolsSet"].ToString();
                txt_Others.Text = dtDetail.Rows[0]["Others"].ToString();
            }

        }


        protected void btn_Save_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Lamps");
            dt.Columns.Add("Mirrors");
            dt.Columns.Add("WheelsTires");
            dt.Columns.Add("Antenna");
            dt.Columns.Add("Radio");
            dt.Columns.Add("CigarLighter");
            dt.Columns.Add("ToolsSet");
            dt.Columns.Add("Others");
            dt.Columns.Add("GeneralID");
            dt.Columns.Add("RSID");

            DataRow dr = dt.NewRow();
            dr["Lamps"] = txt_Lamps.Text;
            dr["Mirrors"] = txt_Mirrors.Text;
            dr["WheelsTires"] = txt_Wheels.Text;
            dr["Antenna"] = txt_Antenna.Text;
            dr["Radio"] = txt_Radio.Text;
            dr["CigarLighter"] = txt_Cigar.Text;
            dr["ToolsSet"] = txt_ToolsSet.Text;
            dr["Others"] = txt_Others.Text;
            dr["GeneralID"] = GeneralID;
            dr["RSID"] = RSID;
            dt.Rows.Add(dr);

            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            Services sv = new Services();
            if (RSID > -1) //update
            {
                int count = sv.UpdateReceptionSheet(dbType, ds);
                if (count != -1)
                    lb_Message.Text = "[Update] Operation complete.";
                else
                    lb_Message.Text = "[Update] Operation failure.";
            }
            else { //add new
               
                RSID  = sv.InsertReceptionSheet(dbType, ds);
                if (RSID != -1)
                { 
                    lb_Message.Text = "[Add New] Operation complete.";
                }else
                    lb_Message.Text = "[Add New] Operation failure.";
            }
        }
    }
}